package com.iplatform.iplatformuser.function.resource.controller;

import com.iplatform.commons.pagination.Pagination;
import com.iplatform.module.privilege.mate.ResInfoBean;
import com.iplatform.iplatformuser.function.resource.service.inter.IResurceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * @author yb
 *
 */
@RestController
@RequestMapping("/function/resource")
@ApiIgnore
public class ResourcesController {

	@Autowired
	private IResurceService service;

	/**
	 * 查询资源表
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@RequestMapping("query")
	public ModelAndView query(HttpServletRequest request, @RequestParam Map<String, String> formMap,
                              @ModelAttribute(value = "pagination") Pagination pagination) {
		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		String dType = formMap.get("dType");
		List<ResInfoBean> resultList = null;
		if("spec".equals(dType)) {
			resultList = service.queryResBySpec(formMap);
		}else if("org".equals(dType)) {
			resultList = service.queryResByOrg(formMap);
		}else {
			resultList = service.queryResBySpec(formMap);
		}
		PageInfo<ResInfoBean> pageInfo = new PageInfo<>(resultList);

		pagination.setTotalCount(pageInfo.getTotal());
		pagination.setTotalPageNum(pageInfo.getPages());

		ModelAndView modelAndView = new ModelAndView("function/resource/list");

		modelAndView.addObject("resourceList", resultList);
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}


	/**
	 * 同步资源表
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@RequestMapping("selsyn")
	public ModelAndView selsyn(HttpServletRequest request, @RequestParam Map<String, String> formMap,
                               @ModelAttribute(value = "pagination") Pagination pagination) {
		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		String dType = formMap.get("dType");
		List<ResInfoBean> resultList = null;
		if("spec".equals(dType)) {
			Integer row2 = service.insertOrUpdateResSpec(formMap);
			resultList = service.queryResBySpec(formMap);
		}else if("org".equals(dType)) {
			Integer row2 = service.insertOrUpdateResOrg(formMap);
			resultList = service.queryResByOrg(formMap);
		}else {
			resultList = service.queryResBySpec(formMap);
		}
		PageInfo<ResInfoBean> pageInfo = new PageInfo<>(resultList);

		pagination.setTotalCount(pageInfo.getTotal());
		pagination.setTotalPageNum(pageInfo.getPages());

		ModelAndView modelAndView = new ModelAndView("function/resource/list");
		modelAndView.addObject("resourceList", resultList);
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

}
